$(function () {

    /*查看用户信息*/
    $(".trContent .see").on("click", function () {
        $("#showHiden").hide().html("");
        $("#myModalLabel").html("");
        $(".modal-body").html("");
        var userId = $(this).parent().parent().find(".userId").html();
        $.ajax({
            url: "/api/user/see",
            data: {
                id: userId
            },
            success: function (data) {
                var role;
                if (data[0].isAdmain) {
                    role = "管理员";
                } else {
                    role = "普通用户";
                }
                var content = "<table class='table table-bordered table-hover'>" +
                    "<tr>" +
                        "<td>用户ID</td>" +
                        "<td>" + data[0]._id + "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>用户名</td>" +
                        "<td>" + data[0].userName + "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>角色</td>" +
                        "<td>" + role + "</td>" +
                    "</tr>" +
                    "</table>";
                $(".modal-body").html(content);
            }
        })
    });
    /*编辑用户信息*/
    $(".trContent .edit").on("click", function () {
        $("#showHiden").show().html("保存").attr("data-id","preserve");
        $("#myModalLabel").html("");
        $(".modal-body").html("");
        var thisPar=$(this).parent().parent();
        var userId = $(thisPar).find(".userId").html();
        $.ajax({
            url: "/api/user/see",
            data: {
                id: userId
            },
            success:function (data) {
                var role;
                var roleA;
                if (data[0].isAdmain) {
                    role = "管理员";
                    roleA="普通用户";
                } else {
                    role = "普通用户";
                    roleA = "管理员";
                }
                var content = "<table class='table table-bordered table-hover'>" +
                    "<tr>" +
                        "<td>用户ID</td>" +
                        "<td>" + data[0]._id + "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>用户名</td>" +
                        "<td class='editSon'>" +
                            "<input type='text' value='"+data[0].userName+"'>" +
                        "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>角色</td>" +
                        "<td class='editSon'>" +
                            "<select name='' id=''>" +
                                "<option value='"+role+"'>"+role+"</option>" +
                                "<option value='"+roleA+"'>"+roleA+"</option>" +
                            "</select>" +
                         "</td>" +
                    "</tr>" +
                    "</table>";
                $(".modal-body").html(content);
                updata(data[0].isAdmain,userId,thisPar,data[0].userName);

            }
        });
        function updata(oldRole,updateId,thisPar,oldUserName) {
            $("#showHiden").on("click", function () {
                if($(this).attr("data-id")=="preserve"){
                    var upadtaData=[];
                    var a=0;
                    $("#myModalLabel").html("");
                    $.each($(".editSon"),function (index,value) {
                        if($($(value).children()[0]).val()!=""){
                            upadtaData.push($($(value).children()[0]).val());
                        }
                        a++;
                    });
                    if(upadtaData.length==a){
                         $.ajax({
                         url:"/api/user/edit",
                         type:"post",
                         data:{
                         id:updateId,
                         oldRole:oldRole,
                         oldUserName:oldUserName,
                         userName:upadtaData[0],
                         isAdmain:upadtaData[1]
                         },
                         dataType:"json",
                         success:function (data) {
                         var isAdmin;
                         if(data.code){
                         $("#myModalLabel").html("<p>"+data.message+"</p>");
                         }else{
                         $("#myModalLabel").html("<p>修改成功</p>");
                         $(thisPar).find(".userName").html(upadtaData[0]);
                         if(upadtaData[1]=="普通用户"){
                         isAdmin="否";
                         }else{
                         isAdmin="是";
                         }
                         $(thisPar).find(".role").html(isAdmin)
                         }

                         }

                         })
                    }else{
                        $("#myModalLabel").html("<p>你输入的值错误！</p>");
                    }

                }
            })
        }


    });
    /*删除一个用户*/
    $(".trContent .del").on("click",function () {
       $("#showHiden").show().html("确认").attr("data-id","confirm");
       $("#myModalLabel").html("");
       $(".modal-body").html("您确认要删除这个用户吗？删除无法找回！");
       var thisPar=$(this).parent().parent();
       var userId = $(thisPar).find(".userId").html();
       var role=$(thisPar).find(".role").get(0).innerText;
       var userRole;

       if(role=="否"){
           userRole="普通用户";
       }else{
           userRole="管理员";
       }


       $("#showHiden").on("click",function () {
           if($(this).attr("data-id")=="confirm"){
               $.ajax({
                   url:"/api/user/del",
                   data:{
                       id:userId,
                       userRole:userRole
                   },
                   dataType:"json",
                   success:function (data) {
                       if(data.code!=200){
                           $("#myModalLabel").html("<p>"+data.message+"</p>");
                       }else {
                           $("#myModalLabel").html("<p>"+data.message+"</p>");
                           setTimeout(function () {
                               location.href="/admin/user";
                           },500)

                       }

                   }
               })
           }
       });
   });
    /*添加一个用户*/
   $(".addUser button").on("click",function () {
       $("#showHiden").show().html("确认").attr("data-id","addUser");
       $("#myModalLabel").html("");
       var addContent="<table class='table table-bordered table-hover addTabUser'>" +
                               "<tr>" +
                                    "<td>用户名：</td>" +
                                    "<td><input type='text' class='inp username'></td>" +
                                "</tr>" +
                               "<tr>" +
                                    "<td>密码</td>" +
                                    "<td><input type='password' class='inp pwd'></td>" +
                                "</tr>" +
                                "<tr>" +
                                    "<td>确认密码</td>" +
                                    "<td><input type='password' class='inp tpwd'></td>" +
                                "</tr>" +
                                "<tr>" +
                                    "<td>角色</td>" +
                                    "<td>" +
                                        "<input type='radio'name='role' class='ordin' checked='checked'>普通用户" +
                                        "<input type='radio' class='admin' name='role'>管理员" +
                                    "</td>" +
                                "</tr>" +
                        "</table>";
       $(".modal-body").html(addContent);
       $(".addTabUser input").on("focus",function () {
           $("#myModalLabel").html("");
       });
       $("#showHiden").on("click",function () {
           $.ajax({
               url:"/api/user/create",
               type:"post",
               data:{
                   userName:$(".username").val(),
                   pwd:$(".pwd").val(),
                   tpwd:$(".tpwd").val(),
                   role:$('input:radio[name="role"]:checked').attr("class")
               },
               dataType:"json",
               success:function (data) {
                   if(data.code!=200){
                       $("#myModalLabel").html("<p>"+data.messages+"</p>");
                   }else{
                       $("#myModalLabel").html("<p>"+data.messages+"</p>");
                       setTimeout(function () {
                           location.href="/admin/user";
                       },1000)
                   }
               }

           })

       })
   });

});